﻿using Bricks.Business.Models.Account;
using Bricks.Business.Data.Repositories.Account;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Bricks.Component.Utils;
using Bricks.Site.Models.Purview;
using Bricks.Business.Data.Repositories.Systems;
using Bricks.Business.Models.Systems;
using Bricks.Site.Models.ParamSetting;
using Bricks.Component.Utils.Const;

namespace Bricks.Business.Service.Impl
{
    [Export(typeof(IParamSettingService))]
    public class ParamSettingService : IParamSettingService
    {
        #region 保护属性
        [Import]
        protected IParamSettingRepository ParamSettingRepository { get; set; }
        #endregion

        #region 公共属性
        public IQueryable<ParamSetting> ParamSettings
        {
            get
            {
                return ParamSettingRepository.Entities;
            }
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 根据Group和Tag获取内容值
        /// </summary>
        /// <param name="group"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public string GetContentByGroupAndTag(string group, string tag)
        {
            string result = string.Empty;
            var paramSetting = ParamSettingRepository.Entities.FirstOrDefault(m => m.Group.Equals(group) && m.Tag.Equals(tag));
            if (paramSetting != null)
            {
                result = paramSetting.Content;
            }
            return result;
        }

        /// <summary>
        /// 保存系统参数
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public OperationResult SaveSystemParam(SystemParamView view)
        {
            //系统名称
            string group = ParamSettingGroup.SystemParam;
            string tag = ParamSettingTag.SystemName;
            string content = view.SystemName;
            OperationResult result = SaveSingleParamSetting(group, tag, content);
            if (result.ResultType == OperationResultType.Success)
            {
                //系统密钥
                tag = ParamSettingTag.SystemKey;
                content = view.SystemKey;
                result = SaveSingleParamSetting(group, tag, content);
                if (result.ResultType == OperationResultType.Success)
                {
                    //版权信息
                    tag = ParamSettingTag.SystemCopyright;
                    content = view.SystemCopyright;
                    result = SaveSingleParamSetting(group, tag, content);
                }
            }
            return result;

        }

        /// <summary>
        /// 保存单条参数记录
        /// </summary>
        /// <param name="group"></param>
        /// <param name="tag"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        private OperationResult SaveSingleParamSetting(string group, string tag, string content)
        {
            var record = ParamSettingRepository.Entities.FirstOrDefault(m => m.Group.Equals(group) && m.Tag.Equals(tag));
            if (record == null)
            {
                ParamSetting newRecord = new ParamSetting();
                newRecord.Group = group;
                newRecord.Tag = tag;
                newRecord.Content = content;
                if (ParamSettingRepository.Insert(newRecord) > 0)
                    return new OperationResult(OperationResultType.Success, "保存成功");
            }
            else
            {
                record.Content = content;
                if (ParamSettingRepository.Update(record) >= 0)
                    return new OperationResult(OperationResultType.Success, "保存成功");
            }
            return new OperationResult(OperationResultType.Error);
        }
        #endregion
    }
}
